import fetch from 'dva/fetch';
import { message } from 'antd';

/**
 * @param  {string} url       T
 * @param  {object} [options]
 * @return {object}           
 */

async function request(url, options) {
    let token = sessionStorage.getItem('isLogin') ? sessionStorage.getItem('isLogin') : ''
    const headers = {
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            'SPR-ZHSQ-Token': token
        }
    };
    const response = await fetch(url, { ...headers, ...options }).catch(err => console.log(err))
    const data = await response.json();
    switch (data.code) {
        case 401: sessionStorage.removeItem('isLogin')
            message.error(data.msg);
            setTimeout(() =>
                window.location.href = "/",
                500);
            break;
        case 0: return { data };
        default: message.error(data.msg);
            break;
    }
}
export default request;
